<?php

define('RANK_TYPE', 1);
define('GROUP_TYPE', 2);
define('USER_TYPE', 3);

class User {
    function User($session_data)
    {
        $this->user_data = $session_data;
    }
    // # Public methods ##
    function get_attribute($attribute)
    {
    	if($attribute == 'user_id' && !isset($this->user_data['user_id']))
    		$attribute = 'id';
    	elseif($attribute == 'id' && !isset($this->user_data['id']))
    		$attribute = 'user_id';
    	if($attribute == 'user_theme')
    		return 'default';
    	if($attribute == 'user_lang')
    		return 'french';
        return isset($this->user_data[$attribute])?$this->user_data[$attribute]:'';
    }

    function get_id()
    {
        return(int)$this->get_attribute('id');
    }

	function get_signature()
	{
		return html_entity_decode($this->get_attribute('signature'), ENT_QUOTES);
	}

    function get_group_color($user_groups, $level = 0)
    {
    	return '';
    }

    function check_level($secure)
    {
    	if(isset($this->user_data['level']) && !empty($this->user_data['level']) && $this->user_data['level'][$secure] == true)
            return true;
        return false;
    }

	function get_high_level()
	{
		$u = "";
		if ($this->check_level(MEMBER_LEVEL))
			$u = MEMBER_LEVEL;
		if ($this->check_level(UPLOADER_LEVEL))
			$u = UPLOADER_LEVEL;
		if ($this->check_level(DONATEUR_LEVEL))
			$u = DONATEUR_LEVEL;
		if ($this->check_level(VALIDEUR_LEVEL))
			$u = VALIDEUR_LEVEL;
		if ($this->check_level(OPERATOR_LEVEL))
			$u = OPERATOR_LEVEL;
		if ($this->check_level(ADMIN_LEVEL))
			$u = ADMIN_LEVEL;
		return $u;
	}

	function set_level($level, $value = true)
	{
		$this->user_data['level'][$level] = $value;
		$this->update_user_level();
	}

	function update_user_level()
	{
		global $Sql;
		$Sql->query_inject("UPDATE " . DB_TABLE_MEMBER . " SET rang = '" . serialise($this->user_data['level']) . "' WHERE id = '" . $this->user_data['id'] . "'", __LINE__, __FILE__);
	}

    function check_auth($array_auth_groups, $authorization_bit)
    {
        if ($this->check_level(ADMIN_LEVEL))
            return true;
        return(bool)($this->_sum_auth_groups($array_auth_groups)&(int)$authorization_bit);
    }

    function check_max_value($key_auth, $max_value_compare = 0)
    {
        if (!is_array($this->groups_auth))
            return false;

        $array_user_auth_groups = $this->_array_group_intersect($this->groups_auth);
        $max_auth = $max_value_compare;
        foreach($array_user_auth_groups as $idgroup => $group_auth) {
            if ($group_auth[$key_auth] == - 1)
                return - 1;
            else
                $max_auth = max($max_auth, $group_auth[$key_auth]);
        }

        return $max_auth;
    }

    function get_groups()
    {
    }

    function set_user_theme($user_theme)
    {
    }

    function update_user_theme($user_theme)
    {
    }

    function set_user_lang($user_lang)
    {
    }

    function update_user_lang($user_lang)
    {
	}
    // # Private methods ##
    function _sum_auth_groups($array_auth_groups)
    {
    }

    function _array_group_intersect($array_auth_groups)
    {
    }
    // # Private attributes ##
    var $user_data;
    var $groups_auth;
    var $user_groups;
}

?>
